package com.blrs.service.qualification.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.blrs.common.core.domain.AjaxResult;
import com.blrs.common.core.domain.TreeSelect;
import com.blrs.common.core.domain.entity.SysDept;
import com.blrs.common.utils.ReflectionUtils;
import com.blrs.common.utils.SecurityUtils;
import com.blrs.common.utils.StringUtils;
import com.blrs.domain.CollaborativeManagement;
import com.blrs.domain.QualificationSwpplier;
import com.blrs.domain.SupplierProduct;
import com.blrs.mapper.CollaborativeManagementMapper;
import com.blrs.mapper.EnterpriseInfoMapper;
import com.blrs.mapper.QualificationSwpplierMapper;
import com.blrs.service.maintype.IMainTypeService;
import com.blrs.service.qualification.IQualificationInfoService;
import com.blrs.system.domain.SysUploadFile;
import com.blrs.system.mapper.SysDictDataMapper;
import com.blrs.system.mapper.SysUploadFileMapper;
import com.blrs.system.mapper.SysUserMapper;
import com.blrs.system.service.ISysDeptService;
import net.sourceforge.pinyin4j.PinyinHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.blrs.mapper.QualificationInfoMapper;
import com.blrs.domain.QualificationInfo;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import static com.blrs.common.utils.PageUtils.startPage;

/**
 * 资质产品管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-08-07
 */
@Service
public class QualificationInfoServiceImpl implements IQualificationInfoService
{
    @Autowired
    private QualificationInfoMapper qualificationInfoMapper;
    @Autowired
    private EnterpriseInfoMapper enterpriseInfoMapper;
    @Autowired
    private SysDictDataMapper sysDictDataMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private IMainTypeService mainTypeService;
    @Autowired
    private SysUploadFileMapper sysUploadFileMapper;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private CollaborativeManagementMapper collaborativeManagementMapper;
    @Autowired
    private QualificationSwpplierMapper qualificationSwpplierMapper;


    /**
     * 查询资质产品管理
     * 
     * @param id 资质产品管理主键
     * @return 资质产品管理
     */
    @Override
    public QualificationInfo selectQualificationInfoById(String id)
    {
        QualificationInfo qualificationInfo = qualificationInfoMapper.selectQualificationInfoById(id);
        //查询关联供应商产品的信息
        List<QualificationSwpplier> qualificationSwppliers=qualificationSwpplierMapper.selectQualificationSwpplierListByProductId(id);
        ArrayList<SupplierProduct> supplierProducts = new ArrayList<>();
        for (QualificationSwpplier qualificationSwpplier : qualificationSwppliers) {
            SupplierProduct supplierProduct = new SupplierProduct();
            supplierProduct.setId(qualificationSwpplier.getSupplierProductId());
            supplierProduct.setProductName(qualificationSwpplier.getProductName());
            supplierProduct.setEnterpriseName(qualificationSwpplier.getOrganizationName());
            supplierProducts.add(supplierProduct);
        }
        qualificationInfo.setSupplierProductList(supplierProducts);
        List<String> enterpriseNames = new ArrayList<>();
        //切割字符串id，转成集合传入
        List<String> list = qualificationInfoMapper.selectEntersList(qualificationInfo.getEnterpriseId().split(","));
        //切割字符串名称集合，转成字符串传入
        qualificationInfo.setEnters(list.stream().collect(Collectors.joining(",")));
        //查询当前组织机构建立的合作方有哪些
        List<CollaborativeManagement> collaborativeManagements=collaborativeManagementMapper.selectCollaborativeManagementListByIsCollaborative(SecurityUtils.getOrgId());
        //过滤作为发起方收集合作对象
        List<CollaborativeManagement> collect = collaborativeManagements.stream()
                .filter(collaborative -> !String.valueOf(SecurityUtils.getOrgId()).equals(collaborative.getInitiatorId()))
                .collect(Collectors.toList());
        if (collect.size()>0){
            for (CollaborativeManagement collaborativeManagement : collect) {
                int count=qualificationInfoMapper.selectQualificationInfoByNameAndOrgId(qualificationInfo.getProductName(),collaborativeManagement.getInitiatorId());
                if (count>0){
                   enterpriseNames.add(collaborativeManagement.getInitiatorName());
                }
            }
        }
        //过滤作为接收方的合作对象
        List<CollaborativeManagement> collect2 = collaborativeManagements.stream()
                .filter(collaborative -> !String.valueOf(SecurityUtils.getOrgId()).equals(collaborative.getRecipientId()))
                .collect(Collectors.toList());
        if (collect2.size()>0){
            for (CollaborativeManagement collaborativeManagement : collect2) {
                int count=qualificationInfoMapper.selectQualificationInfoByNameAndOrgId(qualificationInfo.getProductName(),collaborativeManagement.getRecipientId());
                if (count>0){
                    enterpriseNames.add(collaborativeManagement.getRecipientName());
                }
            }
        }
        //分割字符串
        String enterprises = String.join(",", enterpriseNames);
        qualificationInfo.setEnters(enterprises);
        return qualificationInfo;
    }
    @Override
    public QualificationInfo changeById(String id) {
        QualificationInfo qualificationInfo = qualificationInfoMapper.selectQualificationInfoById(id);
        List<String> enterpriseNames = new ArrayList<>();
        //切割字符串id，转成集合传入
        List<String> list = qualificationInfoMapper.selectEntersList(qualificationInfo.getEnterpriseId().split(","));
        //切割字符串名称集合，转成字符串传入
        qualificationInfo.setEnters(list.stream().collect(Collectors.joining(",")));
        //查询当前组织机构建立的合作方有哪些
        List<CollaborativeManagement> collaborativeManagements=collaborativeManagementMapper.selectCollaborativeManagementListByIsCollaborative(SecurityUtils.getOrgId());
        //过滤作为发起方收集合作对象
        List<CollaborativeManagement> collect = collaborativeManagements.stream()
                .filter(collaborative -> !String.valueOf(SecurityUtils.getOrgId()).equals(collaborative.getInitiatorId()))
                .collect(Collectors.toList());
        if (collect.size()>0){
            for (CollaborativeManagement collaborativeManagement : collect) {
                int count=qualificationInfoMapper.selectQualificationInfoByNameAndOrgId(qualificationInfo.getProductName(),collaborativeManagement.getInitiatorId());
                if (count>0){
                    enterpriseNames.add(collaborativeManagement.getInitiatorName());
                }
            }
        }
        //过滤作为接收方的合作对象
        List<CollaborativeManagement> collect2 = collaborativeManagements.stream()
                .filter(collaborative -> !String.valueOf(SecurityUtils.getOrgId()).equals(collaborative.getRecipientId()))
                .collect(Collectors.toList());
        if (collect2.size()>0){
            for (CollaborativeManagement collaborativeManagement : collect2) {
                int count=qualificationInfoMapper.selectQualificationInfoByNameAndOrgId(qualificationInfo.getProductName(),collaborativeManagement.getRecipientId());
                if (count>0){
                    enterpriseNames.add(collaborativeManagement.getRecipientName());
                }
            }
        }
        //分割字符串
        String enterprises = String.join(",", enterpriseNames);
        qualificationInfo.setEnters(enterprises);
        qualificationInfo.setUserName(SecurityUtils.getUsername());
        qualificationInfo.setReleaseTime(new Date());
        qualificationInfo.setCreationTime(new Date());
        qualificationInfo.setProductRemark(null);
        qualificationInfo.setExeStandard(String.valueOf(new Date().getTime()));
        qualificationInfo.setTemplate(String.valueOf(new Date().getTime()+20));
        qualificationInfo.setProductManual(String.valueOf(new Date().getTime()+40));
        return qualificationInfo;
    }



    /**
     * 查询资质产品管理列表
     * 
     * @param qualificationInfo 资质产品管理
     * @return 资质产品管理
     */
//    @Override
////    public List<QualificationInfo> selectQualificationInfoList(QualificationInfo qualificationInfo) {
////        return qualificationInfoMapper.selectQualificationInfoList(qualificationInfo).stream()
////                .map(info -> {
////                    info.setExeStandardUrl(qualificationInfoMapper.selectUploadUrl(info.getExeStandard()));
////                    info.setTemplateUrl(qualificationInfoMapper.selectUploadUrl(info.getTemplate()));
////                    //切割字符串id，转成集合传入
////                    List<String> list = qualificationInfoMapper.selectEntersList(info.getEnterpriseId().split(","));
////                    //切割字符串名称集合，转成字符串传入
////                    info.setEnters(list.stream().collect(Collectors.joining(",")));
////                    return info;
////                })
////                .collect(Collectors.toList());
////    }
    @Override
    public List<QualificationInfo> selectQualificationInfoList(QualificationInfo qualificationInfo) {
        //List<QualificationInfo> combinedList = new ArrayList<>();
           //查询当前组织机构id是否属于园区
//        if (sysDeptService.selectDeptById(SecurityUtils.getDeptId()).getCompanyType().equals("2")){
//            //如果查询列表中带有条件，进行判断
//            if (Objects.nonNull(qualificationInfo.getProductStatus()) && !"已上架".equals(qualificationInfo.getProductStatus())){
//
//            }
//            else if (Objects.nonNull(qualificationInfo.getExamineStatus()) && !"通过".equals(qualificationInfo.getExamineStatus())){
//
//            }else {
//                //属于园区，查询和该园区建立合作的所有企业的已上架主营产品
//                //查询当前组织机构建立的合作方有哪些
//                List<CollaborativeManagement> collaborativeManagements=collaborativeManagementMapper.selectCollaborativeManagementListByIsCollaborative(SecurityUtils.getOrgId());
//                //过滤作为发起方收集合作对象
//                List<CollaborativeManagement> collect = collaborativeManagements.stream()
//                        .filter(collaborative -> !String.valueOf(SecurityUtils.getOrgId()).equals(collaborative.getInitiatorId()))
//                        .collect(Collectors.toList());
//                if (collect.size()>0){
//                    for (CollaborativeManagement collaborativeManagement : collect) {
//                        //查询发起方的所有上架主营产品
//                        qualificationInfo.setOrganizationId(Long.valueOf(collaborativeManagement.getInitiatorId()));
//                        List<QualificationInfo> list=qualificationInfoMapper.selectQualificationInfoListByUp(qualificationInfo);
//                        combinedList.addAll(list);
//                    }
//                }
//                //过滤作为接收方的合作对象
//                List<CollaborativeManagement> collect2 = collaborativeManagements.stream()
//                        .filter(collaborative -> !String.valueOf(SecurityUtils.getOrgId()).equals(collaborative.getRecipientId()))
//                        .collect(Collectors.toList());
//                if (collect2.size()>0){
//                    for (CollaborativeManagement collaborativeManagement : collect2) {
//                        //查询发起方的所有上架主营产品
//                        qualificationInfo.setOrganizationId(Long.valueOf(collaborativeManagement.getRecipientId()));
//                        List<QualificationInfo> list=qualificationInfoMapper.selectQualificationInfoListByUp(qualificationInfo);
//                        combinedList.addAll(list);
//                    }
//                }
//            }
//        }
        if(!Objects.nonNull(qualificationInfo.getOrganizationId())){
            qualificationInfo.setOrganizationId(SecurityUtils.getOrgId());
        }

        List<QualificationInfo> qualificationInfoList = qualificationInfoMapper.selectQualificationInfoList(qualificationInfo);
//        combinedList.addAll(qualificationInfoList);
        return qualificationInfoList;
    }

    @Override
    public List<QualificationInfo> selectQualificationInfoListByOrgId(QualificationInfo qualificationInfo) {
        return qualificationInfoMapper.selectQualificationInfoListByOrgId(qualificationInfo);
    }

    @Override
    public List<QualificationInfo> selectQualificationInfoChangeList(QualificationInfo qualificationInfo) {
       //查询出所有除开自身所有上架的主营产品
//        qualificationInfo.setOrganizationId(SecurityUtils.getOrgId());

//        //查询自身所有产品编号(因为编号是唯一的，避免已经选择过的再次进行选择)
//        List<String> list=qualificationInfoMapper.selectEntersNo(SecurityUtils.getOrgId());
//        startPage();
//        qualificationInfo.setProductNos(list);
        return qualificationInfoMapper.selectQualificationInfoListChange(qualificationInfo);
    }

    @Override
    public int selectQualificationInfoNumber(Long organizationId) {
        return qualificationInfoMapper.selectQualificationInfoNumber(organizationId);
    }

    @Override
    public int revokeProductById(String id) {
        return qualificationInfoMapper.updateQualificationStatusById(id);
    }

    @Override
    public List<QualificationInfo> selectQualificationInfoListByListing(QualificationInfo qualificationInfo) {
        List<QualificationInfo> list=qualificationInfoMapper.selectQualificationInfoByListing(qualificationInfo);
        for (QualificationInfo info : list) {
            SysUploadFile uploadFile = new SysUploadFile();
            uploadFile.setBusinessCode("qualification");
            uploadFile.setBusinessId(info.getTemplate());
           // sysUploadFileMapper.selectUrlByCode("qualification", info.getTemplate())
            List<SysUploadFile> sysUploadFiles = sysUploadFileMapper.selectSysUploadFileList(uploadFile);
            if (sysUploadFiles.size() > 0) {
                info.setTemplateUrl(sysUploadFiles.get(0).getUrl());
            }
            info.setProductType(sysDictDataMapper.selectDictLabel("product_type",info.getProductType()));
        }
        return list;
    }

    @Override
    public QualificationInfo selectQualificationInfoByIdAndOrg(String id) {
        QualificationInfo qualificationInfo = qualificationInfoMapper.selectQualificationInfoById(id);
        qualificationInfo.setProductType(sysDictDataMapper.selectDictLabel("product_type",qualificationInfo.getProductType()));
        qualificationInfo.setSysDept(sysDeptService.selectDeptById(qualificationInfo.getOrganizationId()));
        SysDept sysDept = qualificationInfo.getSysDept();
        sysDept.setNature(sysDictDataMapper.selectDictLabel("enterprise_nature",sysDept.getNature()));
        if(sysDept.getCompanyType().equals("0")){
            //说明是企业
            sysDept.setIndustry(sysDictDataMapper.selectDictLabel("customer_industry",sysDept.getIndustry()));
        }else if (sysDept.getCompanyType().equals("1")){
            //说明是机构
            sysDept.setIndustry(sysDictDataMapper.selectDictLabel("customer_industry_serve",sysDept.getIndustry()));
        }else {
            //否作就是园区
            sysDept.setIndustry(sysDictDataMapper.selectDictLabel("customer_industry_garden",sysDept.getIndustry()));
        }
        qualificationInfo.setSysDept(sysDept);
        return  qualificationInfo;
    }

    @Override
    public String selectQualificationNo(String no, String id) {
        // 查询数据库中符合条件的最大编号
        String maxNo = qualificationInfoMapper.selectQualificationInfoByOrgAndNo(SecurityUtils.getOrgId(), no);

        if (Objects.isNull(id) || "null".equals(id)) { // 新增
            return generateNextNumber(no, maxNo);
        } else { // 修改
            QualificationInfo qualificationInfo = qualificationInfoMapper.selectQualificationInfoById(id);
            if (qualificationInfo.getProductType().equals(no)) {
                // 说明换回去了
                return qualificationInfo.getProductNo();
            } else {
                return generateNextNumber(no, maxNo);
            }
        }
    }

    @Override
    public int updateQualificationInfoApply(String id) {
        //先查询这条记录是启动状态还是停用状态
        QualificationInfo qualificationInfo = qualificationInfoMapper.selectQualificationInfoById(id);
        if (qualificationInfo.getExamineStatus().equals("停用")){
            //说明这次是启用
            return qualificationInfoMapper.updateQualificationInfoApply(id,"已发布");
        }else {
            return qualificationInfoMapper.updateQualificationInfoApply(id,"停用");
        }
    }



    private String generateNextNumber(String prefix, String maxNo) {
        if (Objects.isNull(maxNo)) {
            // 如果没有找到最大的编号，返回初始编号后加上"0001"
            return prefix + "0001";
        } else {
            // 提取编号中的数字部分
            String numericPart = maxNo.substring(prefix.length());
            int sequence = Integer.parseInt(numericPart) + 1;

            // 生成下一个编号，保持与初始编号相同的前缀，并用零填充
            return prefix + String.format("%04d", sequence); // 假设最多4位数字（0001到9999）
        }
    }



    @Override
    public List<QualificationInfo> selectQualificationInfoListAll(QualificationInfo qualificationInfo) {
        if (Objects.nonNull(qualificationInfo.getOrganizationId()) && qualificationInfo.getOrganizationId()==1L){
            qualificationInfo.setOrganizationId(null);
        }
        if (Objects.nonNull(qualificationInfo.getProductStatus()) && !"已上架".equals(qualificationInfo.getProductStatus())){
            return Collections.emptyList();
        }
        //2025-03-14 修改成
        List<QualificationInfo> qualificationInfoList = qualificationInfoMapper.selectQualificationInfoListAdmin(qualificationInfo);
        for (QualificationInfo info : qualificationInfoList) {
            // 切割字符串id，转成集合传入
            List<String> list = qualificationInfoMapper.selectEntersList(info.getEnterpriseId().split(","));
//            info.setTemplateUrl(sysUploadFileMapper.selectUrlByCode("qualification", info.getTemplate()));
//            info.setExeStandardUrl(sysUploadFileMapper.selectUrlByCode("exeStandard", info.getExeStandard()));
            // 切割字符串名称集合，转成字符串传入
            info.setEnters(String.join(",", list));
        }
        return qualificationInfoList;
    }

    @Override
    public AjaxResult selectQualificationInfoByType() {
        List<SysDept> deptList = sysDeptService.selectDeptList();
        ArrayList<TreeSelect> treeSelects = new ArrayList<>();
        for (SysDept sysDept : deptList) {
            if (!sysDept.getDeptName().equals("见实力工业企业云平台")){
                TreeSelect treeSelect = new TreeSelect();
            treeSelect.setLabel(sysDept.getDeptName());
            treeSelect.setId(sysDept.getDeptId());
            treeSelects.add(treeSelect);
            }
        }
        //先创建一个父节点
        ArrayList<TreeSelect> treeSelects1 = new ArrayList<>();
        TreeSelect parentTree = new TreeSelect();
        parentTree.setId(1L);
        parentTree.setChildren(treeSelects);
        parentTree.setLabel("全部");
        treeSelects1.add(parentTree);
        return AjaxResult.success(treeSelects1);
    }

    @Override
    public AjaxResult releaseProduct(String[] id) {
        qualificationInfoMapper.updateQualificationInfoStatus(id);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult updateQualificationInfoExamine(QualificationInfo qualificationInfo) {
        qualificationInfoMapper.updateQualificationInfoExamine(qualificationInfo);
        return AjaxResult.success();
    }



    /**
     * 新增资质产品管理
     * 
     * @param qualificationInfo 资质产品管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertQualificationInfo(QualificationInfo qualificationInfo)
    {
        qualificationInfo.setOrganizationId(SecurityUtils.getOrgId());
        try {
            //产品编号做重复性修改
            if (qualificationInfoMapper.selectOneProductNo(qualificationInfo)>0){
             return 0;
            }
            ReflectionUtils.setCommonPrivateProperty(qualificationInfo);
            qualificationInfo.setOrganizationName(SecurityUtils.getOrgName());
            qualificationInfo.setCreateId(sysUserMapper.selectUserByUserName(qualificationInfo.getUserName()).getUserId());
            // 判断是否变更，设置相应的状态
            boolean isChanged = qualificationInfo.getIsChange() > 0;
            qualificationInfo.setExamineStatus("未审核");
            qualificationInfo.setProductStatus("未上架");
            qualificationInfo.setOrganizationId(SecurityUtils.getOrgId());
            qualificationInfo.setOrganizationName(SecurityUtils.getOrgName());
            String supplierProductId = qualificationInfo.getSupplierProductId();
            String[] split=new String[0];
            if (StringUtils.isNotEmpty(supplierProductId)) {
                split = qualificationInfo.getSupplierProductId().split(",");
                for (String s : split) {
                    QualificationSwpplier qualificationSwpplier = new QualificationSwpplier();
                    qualificationSwpplier.setProductId(qualificationInfo.getId());
                    qualificationSwpplier.setSupplierProductId(s);
                    qualificationSwpplierMapper.insertQualificationSwpplier(qualificationSwpplier);
                }
            }
            if (isChanged){
               //如果是选择已有产品，那么去判断产品的类型的编号最大值（当前机构的）
                String maxNo = selectQualificationNo(qualificationInfo.getProductType(), null);
                qualificationInfo.setProductNo(maxNo);
            }
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        return qualificationInfoMapper.insertQualificationInfo(qualificationInfo);
    }

    public static String getFirstLetter(String chinese) {
        if (chinese == null || chinese.trim().isEmpty()) {
            return "";
        }
    StringBuilder pinyinFirstLetter = new StringBuilder();
        char[] chars = chinese.toCharArray();
        for (char ch : chars) {
            // 使用pinyin4j处理单个字符
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(ch);
            if (pinyinArray != null) {
                // 取拼音的第一个字母
                pinyinFirstLetter.append(pinyinArray[0].charAt(0));
            } else {
                // 如果不是汉字字符，直接加入原字符
                pinyinFirstLetter.append(ch);
            }
        }
        return pinyinFirstLetter.toString().toUpperCase();
    }

    /**
     * 修改资质产品管理
     * 
     * @param qualificationInfo 资质产品管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateQualificationInfo(QualificationInfo qualificationInfo)
    {
        try {
            ReflectionUtils.setCommonPrivateProperty(qualificationInfo);
            //判断是否有修改成相同产品
            if (qualificationInfoMapper.selectIdenticalQualificationInfo(qualificationInfo)>0){
                return 0;
            }
            //查询一下修改的这条数据是否改动了产品状态，如果确定修改了 那么就需要赋值为未审批状态
            String productStatus = qualificationInfoMapper.selectQualificationInfoById(qualificationInfo.getId()).getProductStatus();
            if (!productStatus.equals(qualificationInfo.getProductStatus())){
                qualificationInfo.setApprove(0);
            }
            //先删除改产品所有的关联的供应商产品
            qualificationSwpplierMapper.deleteQualificationSwpplierByProductId(qualificationInfo.getId());
            //再插入新的关联的供应商产品
            if (StringUtils.isNotEmpty(qualificationInfo.getSupplierProductId())) {
                String[] split = qualificationInfo.getSupplierProductId().split(",");
                if (split.length>0){
                    for (String s : split) {
                        QualificationSwpplier qualificationSwpplier = new QualificationSwpplier();
                        qualificationSwpplier.setProductId(qualificationInfo.getId());
                        qualificationSwpplier.setSupplierProductId(s);
                        qualificationSwpplierMapper.insertQualificationSwpplier(qualificationSwpplier);
                    }
                }
            }
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        return qualificationInfoMapper.updateQualificationInfo(qualificationInfo);
    }

    /**
     * 批量删除资质产品管理
     * 
     * @param ids 需要删除的资质产品管理主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteQualificationInfoByIds(String[] ids)
    {
        //删除所有关联供应商产品
        for (String id : ids) {
            qualificationSwpplierMapper.deleteQualificationSwpplierByProductId(id);
        }
        return qualificationInfoMapper.deleteQualificationInfoByIds(ids);
    }

    /**
     * 删除资质产品管理信息
     * 
     * @param id 资质产品管理主键
     * @return 结果
     */
    @Override
    public int deleteQualificationInfoById(String id)
    {
        return qualificationInfoMapper.deleteQualificationInfoById(id);
    }

    /**
     * 导入数据库中
     *
     * @param list
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult importExcel(@Validated  List<QualificationInfo> list) throws Exception {
        for (QualificationInfo qualificationInfo : list) {
            if (Objects.isNull(qualificationInfo.getProductType())){
                return AjaxResult.error("主营产品类型不能为空");
            }
            if(Objects.isNull(qualificationInfo.getProductNo())){
                return AjaxResult.error("主营产品编号不能为空");
            }
//            String s = sysDictDataMapper.selectDictLabel("product_type", qualificationInfo.getProductType());
//             if (Objects.isNull(s)){
//                 qualificationInfo.setProductNo(getFirstLetter(s)+(System.currentTimeMillis() / (1000*60)));
//             }
                ReflectionUtils.setCommonPrivateProperty(qualificationInfo);
                qualificationInfo.setOrganizationId(SecurityUtils.getOrgId());
            qualificationInfo.setOrganizationName(SecurityUtils.getOrgName());
                //必须给执行文件和样板附默认值
               qualificationInfo.setExeStandard(String.valueOf(new Date().getTime()));
                qualificationInfo.setTemplate(String.valueOf(new Date().getTime()));
                qualificationInfo.setCreateId(SecurityUtils.getUserId());
                /**
                 * 查询是否存在
                 */
                int i=qualificationInfoMapper.selectQualificationInfoByNameAndNo(qualificationInfo);
                if (i>0){
                    throw new RuntimeException("导入失败,"+qualificationInfo.getProductName()+"数据编号"+qualificationInfo.getProductNo()+"发生重复！！！");
                }
                //插入数据
            //获取当前用户
          qualificationInfo.setCreateId(SecurityUtils.getUserId());
                qualificationInfo.setCreationTime(new Date());
            qualificationInfoMapper.insertQualificationInfo(qualificationInfo);
        }

        return AjaxResult.success("导入成功,成功导入"+list.size()+"条数据");
    }

    //根据产品类别查询字典中文
    @Override
    public String selectProductType(String type) {
    return qualificationInfoMapper.selectType(type);
    }

    @Override
    public int approve(String[] id, String status) {
     return    qualificationInfoMapper.approveStatus(id,status);
    }

}
